package com.qf.mybatis.test;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author 郑大仙丶
 * @version 1.0
 * @date 2021/3/16 11:56
 */
public class Demo1 {

    @Test
    public void findCount() throws IOException {
        // 需要构建工厂的材料，MyBatis的核心配置文件
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");

        // SqlSessionFactoryBuilder专门构建工厂，Builder可以直接new
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);

        // SqlSessionFactory专门生产SqlSession
        SqlSession sqlSession = factory.openSession();

        //MyBatis和数据库交互的核心对象叫 SqlSession
        Integer count = sqlSession.selectOne("user.findCount");   // IBatis的方式
        System.out.println("user表中有：" + count + "条数据。");
    }

}
